#include "../lib/Affichage.hpp"
#include "../lib/Carte.hpp"
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cassert>


void Rafraichit_ecran(SDL_Surface * ecran, Partie * P)
{
    SDL_Surface *im_colon = NULL, *im_colon_ia = NULL, *im_guerrier = NULL, *im_guerrier_ia = NULL, *im_ville = NULL, *im_ville_ia = NULL;


    im_colon = SDL_LoadBMP("data/colon.bmp");
    im_colon_ia = SDL_LoadBMP("data/colonIA.bmp");
    im_guerrier = SDL_LoadBMP("data/guerrier.bmp");
    im_guerrier_ia = SDL_LoadBMP("data/guerrierIA.bmp");
    im_ville = SDL_LoadBMP("data/ville.bmp");
    im_ville_ia = SDL_LoadBMP("data/villeIA.bmp");


    Affiche_Carte_Sdl(ecran);


    Affiche_Unites_Joueur (ecran,im_colon,im_guerrier, P->Jhumain);
    Affiche_Unites_Joueur (ecran,im_colon_ia,im_guerrier_ia, P->IA);

    Affiche_Villes_Joueur (ecran,im_ville,P->Jhumain);
    Affiche_Villes_Joueur (ecran,im_ville_ia,P->IA);

    SDL_Flip(ecran);

    SDL_FreeSurface(im_colon);
    SDL_FreeSurface(im_colon_ia);
    SDL_FreeSurface(im_guerrier);
    SDL_FreeSurface(im_guerrier_ia);
    SDL_FreeSurface(im_ville);
    SDL_FreeSurface(im_ville_ia);


}

void Affiche_Cercle_Selection(SDL_Surface * ecran, Coord * C)
{
    SDL_Surface * im_cercle = NULL;
    SDL_Rect pos_cercle;

    pos_cercle.x = (Retourne_X (C))*64;
    pos_cercle.y = (Retourne_Y (C))*64;

    im_cercle = SDL_LoadBMP("data/selection.bmp");

    SDL_SetColorKey(im_cercle, SDL_SRCCOLORKEY, SDL_MapRGB(im_cercle->format, 255, 255, 255)); // applique la transparence
    SDL_BlitSurface(im_cercle, NULL, ecran, &pos_cercle);

    SDL_Flip(ecran);

    SDL_FreeSurface(im_cercle);
}



void Affiche_Unites_Joueur (SDL_Surface * ecran, SDL_Surface * im_colon, SDL_Surface * im_guerrier, Joueur * J)
{
    SDL_Rect pos_unite;
    Coord * C;
    Unite * U;

    for(unsigned int i=0 ; i < J->nb_unite ; i++)
    {
        U = Retourne_Unite(J,i);
        C = Retourne_Coord (U);

        pos_unite.x = (Retourne_X (C))*64;
        pos_unite.y = (Retourne_Y (C))*64;


        switch (U->nom)
            {
                case colon:
                    SDL_SetColorKey(im_colon, SDL_SRCCOLORKEY, SDL_MapRGB(im_colon->format, 255, 255, 255)); // applique la transparence
                    SDL_BlitSurface(im_colon, NULL, ecran, &pos_unite);
                    break;
                case guerrier:
                    SDL_SetColorKey(im_guerrier, SDL_SRCCOLORKEY, SDL_MapRGB(im_guerrier->format, 255, 255, 255)); // applique la transparence
                    SDL_BlitSurface(im_guerrier, NULL, ecran, &pos_unite);
                    break;
                default:
                    break;
            }

    }

    SDL_Flip(ecran);

}

void Affiche_Villes_Joueur (SDL_Surface * ecran, SDL_Surface * im_ville, Joueur * J)
{
    SDL_Rect pos_ville;
    Coord * C;
    Ville * V;

    for(unsigned int i=0 ; i < J->nb_ville ; i++)
    {
        V = Retourne_Ville (J,i);
        C = Retourne_Coord_Ville (V);

        pos_ville.x = (Retourne_X (C))*64;
        pos_ville.y = (Retourne_Y (C))*64;

        SDL_SetColorKey(im_ville, SDL_SRCCOLORKEY, SDL_MapRGB(im_ville->format, 255, 255, 255)); // applique la transparence
        SDL_BlitSurface(im_ville, NULL, ecran, &pos_ville);


    }

    SDL_Flip(ecran);

}

void Affiche_Carte_Sdl(SDL_Surface * ecran)
{
    Carte CA;
    Init_Carte (CA);

    Init_Carte_Fichier (CA);

    SDL_Surface *case_terre = NULL, *case_mer = NULL;

    SDL_Rect pos_case;


    pos_case.x = 0;
    pos_case.y = 0;

    case_terre = SDL_LoadBMP("data/caseterre.bmp");
    case_mer = SDL_LoadBMP("data/caseeau.bmp");

    for ( int j = 0 ; j < CA.taille; j++)
    {
        pos_case.y = (j*64);

        for ( int k = 0 ; k < CA.taille ; k ++)
        {

             pos_case.x = (k*64);

            switch (CA.CARTE[j+(k*CA.taille)])
            {
                case prairie:
                    SDL_BlitSurface(case_terre, NULL, ecran, &pos_case);
                    break;
                case mer:
                    SDL_BlitSurface(case_mer, NULL, ecran, &pos_case);
                    break;
                default:
                    break;
            }

        }
    }

SDL_Flip(ecran);

SDL_FreeSurface(case_terre);
SDL_FreeSurface(case_mer);


}

void Affiche_Menu_Unite(SDL_Surface * ecran)
{

    SDL_Surface *im_fond = NULL, *im_ville = NULL, *im_creer_ville = NULL, *im_deplacements = NULL, *im_attaquer = NULL, *im_gauche = NULL, *im_haut = NULL, *im_bas = NULL, *im_droite = NULL, *im_rien = NULL, *im_sauver = NULL, *im_quitter = NULL;

    SDL_Rect pos_fond, pos_ville, pos_creer_ville, pos_deplacements, pos_attaquer, pos_gauche_d, pos_haut_d, pos_bas_d, pos_droite_d, pos_gauche_a, pos_haut_a, pos_bas_a, pos_droite_a, pos_rien, pos_sauver, pos_quitter;


    pos_fond.x = 768;
    pos_fond.y = 0;
    pos_ville.x = 768;
    pos_ville.y = 0;
    pos_creer_ville.x = 832;
    pos_creer_ville.y = 0;
    pos_deplacements.x = 768;
    pos_deplacements.y = 64;
    pos_attaquer.x = 768;
    pos_attaquer.y = 192;

    pos_gauche_d.x = 768;
    pos_gauche_d.y = 128;
    pos_haut_d.x = 832;
    pos_haut_d.y = 128;
    pos_bas_d.x = 896;
    pos_bas_d.y = 128;
    pos_droite_d.x = 960;
    pos_droite_d.y = 128;

    pos_gauche_a.x = 768;
    pos_gauche_a.y = 256;
    pos_haut_a.x = 832;
    pos_haut_a.y = 256;
    pos_bas_a.x = 896;
    pos_bas_a.y = 256;
    pos_droite_a.x = 960;
    pos_droite_a.y = 256;

    pos_rien.x = 768;
    pos_rien.y = 320;

    pos_sauver.x = 768;
    pos_sauver.y = 640;
    pos_quitter.x = 768;
    pos_quitter.y = 704;


    im_fond = SDL_LoadBMP("data/fondmenu.bmp");
    im_ville = SDL_LoadBMP("data/ville.bmp");
    im_creer_ville = SDL_LoadBMP("data/creerville.bmp");
    im_deplacements = SDL_LoadBMP("data/deplacements.bmp");
    im_attaquer = SDL_LoadBMP("data/attaquer.bmp");
    im_gauche = SDL_LoadBMP("data/flechegauche.bmp");
    im_haut = SDL_LoadBMP("data/flechehaut.bmp");
    im_bas = SDL_LoadBMP("data/flechebas.bmp");
    im_droite = SDL_LoadBMP("data/flechedroite.bmp");
    im_rien = SDL_LoadBMP("data/rien.bmp");
    im_sauver = SDL_LoadBMP("data/sauvegarder.bmp");
    im_quitter = SDL_LoadBMP("data/quitter.bmp");



    SDL_BlitSurface(im_fond, NULL, ecran, &pos_fond);

    SDL_SetColorKey(im_ville, SDL_SRCCOLORKEY, SDL_MapRGB(im_ville->format, 255, 255, 255));
    SDL_BlitSurface(im_ville, NULL, ecran, &pos_ville);
    SDL_BlitSurface(im_creer_ville, NULL, ecran, &pos_creer_ville);
    SDL_BlitSurface(im_deplacements, NULL, ecran, &pos_deplacements);
    SDL_BlitSurface(im_attaquer, NULL, ecran, &pos_attaquer);

    SDL_BlitSurface(im_gauche, NULL, ecran, &pos_gauche_d);
    SDL_BlitSurface(im_haut, NULL, ecran, &pos_haut_d);
    SDL_BlitSurface(im_bas, NULL, ecran, &pos_bas_d);
    SDL_BlitSurface(im_droite, NULL, ecran, &pos_droite_d);
    SDL_BlitSurface(im_gauche, NULL, ecran, &pos_gauche_a);
    SDL_BlitSurface(im_haut, NULL, ecran, &pos_haut_a);
    SDL_BlitSurface(im_bas, NULL, ecran, &pos_bas_a);
    SDL_BlitSurface(im_droite, NULL, ecran, &pos_droite_a);
    SDL_BlitSurface(im_rien, NULL, ecran, &pos_rien);
    SDL_BlitSurface(im_sauver, NULL, ecran, &pos_sauver);
    SDL_BlitSurface(im_quitter, NULL, ecran, &pos_quitter);

    SDL_Flip(ecran);

    SDL_FreeSurface(im_fond);
    SDL_FreeSurface(im_ville);
    SDL_FreeSurface(im_creer_ville);
    SDL_FreeSurface(im_deplacements);
    SDL_FreeSurface(im_attaquer);
    SDL_FreeSurface(im_gauche);
    SDL_FreeSurface(im_haut);
    SDL_FreeSurface(im_bas);
    SDL_FreeSurface(im_droite);
    SDL_FreeSurface(im_rien);
    SDL_FreeSurface(im_sauver);
    SDL_FreeSurface(im_quitter);

}

void Affiche_Menu_Ville(SDL_Surface * ecran)
{

    SDL_Surface *im_fond = NULL, *im_colon = NULL, *ImCreerColon = NULL, *im_guerrier = NULL, *ImCreerGuerrier = NULL, *im_rien = NULL, *im_sauver = NULL, *im_quitter = NULL;

    SDL_Rect pos_fond, pos_colon, pos_creer_colon, pos_guerrier, pos_creer_guerrier, pos_rien, pos_sauver, pos_quitter;


    pos_fond.x = 768;
    pos_fond.y = 0;
    pos_colon.x = 768;
    pos_colon.y = 0;
    pos_creer_colon.x = 832;
    pos_creer_colon.y = 0;
    pos_guerrier.x = 768;
    pos_guerrier.y = 64;
    pos_creer_guerrier.x = 832;
    pos_creer_guerrier.y = 64;

    pos_rien.x = 768;
    pos_rien.y = 128;

    pos_sauver.x = 768;
    pos_sauver.y = 640;
    pos_quitter.x = 768;
    pos_quitter.y = 704;


    im_fond = SDL_LoadBMP("data/fondmenu.bmp");
    im_colon = SDL_LoadBMP("data/colon.bmp");
    ImCreerColon = SDL_LoadBMP("data/creercolon.bmp");
    im_guerrier = SDL_LoadBMP("data/guerrier.bmp");
    ImCreerGuerrier = SDL_LoadBMP("data/creerguerrier.bmp");
    im_rien = SDL_LoadBMP("data/rien.bmp");
    im_sauver = SDL_LoadBMP("data/sauvegarder.bmp");
    im_quitter = SDL_LoadBMP("data/quitter.bmp");


    SDL_BlitSurface(im_fond, NULL, ecran, &pos_fond);

    SDL_SetColorKey(im_colon, SDL_SRCCOLORKEY, SDL_MapRGB(im_colon->format, 255, 255, 255));
    SDL_BlitSurface(im_colon, NULL, ecran, &pos_colon);
    SDL_BlitSurface(ImCreerColon, NULL, ecran, &pos_creer_colon);
    SDL_SetColorKey(im_guerrier, SDL_SRCCOLORKEY, SDL_MapRGB(im_guerrier->format, 255, 255, 255));
    SDL_BlitSurface(im_guerrier, NULL, ecran, &pos_guerrier);
    SDL_BlitSurface(ImCreerGuerrier, NULL, ecran, &pos_creer_guerrier);
    SDL_BlitSurface(im_rien, NULL, ecran, &pos_rien);
    SDL_BlitSurface(im_sauver, NULL, ecran, &pos_sauver);
    SDL_BlitSurface(im_quitter, NULL, ecran, &pos_quitter);

    SDL_Flip(ecran);

    SDL_FreeSurface(im_fond);
    SDL_FreeSurface(im_colon);
    SDL_FreeSurface(ImCreerColon);
    SDL_FreeSurface(im_guerrier);
    SDL_FreeSurface(ImCreerGuerrier);
    SDL_FreeSurface(im_rien);
    SDL_FreeSurface(im_sauver);
    SDL_FreeSurface(im_quitter);

}

void Affiche_Menu_Unite_IA(SDL_Surface * ecran)
{

    SDL_Surface *im_fond = NULL;

    SDL_Rect pos_fond;


    pos_fond.x = 768;
    pos_fond.y = 0;


    im_fond = SDL_LoadBMP("data/fondmenuIA.bmp");

    SDL_BlitSurface(im_fond, NULL, ecran, &pos_fond);

    SDL_Flip(ecran);

    SDL_FreeSurface(im_fond);


}

void Affiche_Menu_Ville_IA(SDL_Surface * ecran)
{

    SDL_Surface *im_fond = NULL;

    SDL_Rect pos_fond;


    pos_fond.x = 768;
    pos_fond.y = 0;


    im_fond = SDL_LoadBMP("data/fondmenuIA.bmp");


    SDL_BlitSurface(im_fond, NULL, ecran, &pos_fond);


    SDL_Flip(ecran);

    SDL_FreeSurface(im_fond);

}




